
import _ from 'lodash'
import * as userProxy from '../../proxys/user'
import { CustomError, CODE, ErrorInfo } from '../../error'
import * as mailer from '../../utils/mailer'
import { site_name } from '../../config'
import { isAdmins, ADMINS_GROUP_NAME, USERS_GROUP_NAME } from '../../middlewares/auth'

export const getlist = (data, req, res, next) => {
  let { query, options, limit, skip } = data
  let { page } = options
  return userProxy.getList(query, null, null, limit, skip)
    .then( ret => {
      let { counts, list } = ret
      return res.api({ 
        list, 
        options: { ...options, counts, page }
      }) 
    })
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const create = (data, req, res, next) => {
  let { info, query, options, limit, skip } = data
  let { password } = info
  let { page } = options
  return userProxy.createUser(info)
    .then( ret => {
      if (process.env.NODE_ENV === 'production') {
        let user_show_name = _.isEmpty(ret.nickname) ? ret.username : ret.nickname
        let user_type = isAdmins(ret.group.level) ? ADMINS_GROUP_NAME : USERS_GROUP_NAME
        mailer.sendMjml('create_user.mjml', { 
          to: `${user_show_name} <${ret.email}>`, 
          subject: `欢迎加入${site_name}` 
        }, {
          site_name,
          user_show_name,
          user_type ,
          username: ret.username,
          password,
          email: ret.email
        })
      }
      return userProxy.getList(query, null, null, limit, skip)
    })
    .then( ret => {
      let { counts, list } = ret
      return res.api({ 
        list, 
        options: { ...options, counts, page }
      })
    })
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const findUser = (data, req, res, next) => {
  let { query, type } = data
  let typeName = {
    username: '用户名',
    email: 'E-Mail'
  }
  return userProxy.getInfo(query)
    .then( ret => {
      if (ret) {
        return res.api(null, CODE.ERROR_VALID_USERNAME_OCCUPY, [typeName[type]])
      }
      return res.api(null)
    })
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const edit = (data, req, res, next) => {
  let { _id, info } = data
  return userProxy.editUser({ _id }, info)
    .then( ret => res.api(ret) )
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const remove = (data, req, res, next) => {
  let { remove, query, options, limit, skip } = data
  let { page } = options
  return userProxy.removeUser(remove)
    .then( ret => userProxy.getList(query, null, null, limit, skip) )
    .then( ret => {
      let { counts, list } = ret
      return res.api({ 
        list, 
        options: { ...options, counts, page }
      })
    })
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}

export const setStatus = (data, req, res, next) => {
  let { update, info, query, options, limit, skip } = data
  let { page } = options
  return userProxy.updateUserInfo(update, info)
    .then( ret => userProxy.getList(query, null, null, limit, skip) )
    .then( ret => {
      let { counts, list } = ret
      return res.api({ 
        list, 
        options: { ...options, counts, page }
      })
    })
    .catch(CustomError, err =>  res.api(null, err.code) )
    .catch( err => next(err) )
}